package com.dmap.admin.service.backgroundv2.impl;

import com.dmap.admin.constants.AdminConstant;
import com.dmap.admin.controller.backgroundv2.param.LoginLogSelectParam;
import com.dmap.admin.dao.LoginLogMapper;
import com.dmap.admin.po.LoginLog;
import com.dmap.admin.po.LoginLogExample;
import com.dmap.admin.service.backgroundv2.LoginLogService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * \* User: helei
 * \* Date: 04/05/18
 * \* Description:登陆日志相关接口实现类
 * \
 */
@Service
public class LoginLogServiceImpl implements LoginLogService {

    @Resource
    private LoginLogMapper loginLogMapper;

    @Override
    public LoginLog insertLoginLog(LoginLog loginLog) {
        int success = loginLogMapper.insertSelective(loginLog);
        return (success == 1) ? loginLog : null;
    }

    @Override
    public List<LoginLog> selectByLoginLogSelectParam(LoginLogSelectParam param) {
        LoginLogExample example = new LoginLogExample();
        example.createCriteria().andUserIdEqualTo(Long.valueOf(param.getUserId()));
        example.setOrderByClause(AdminConstant.CREATE_TIME + AdminConstant.DESC);
        example.setLimitStart((param.getPageIndex() - 1) * param.getPageSize());
        example.setLimitEnd(param.getPageSize());
        return loginLogMapper.selectByExample(example);
    }

    @Override
    public int countByUserId(Long userId) {
        LoginLogExample example = new LoginLogExample();
        example.createCriteria().andUserIdEqualTo(userId);
        return loginLogMapper.countByExample(example);
    }

    @Override
    public LoginLog selectLastLogByUserId(Long userId) {
        LoginLog loginLog = null;
        LoginLogExample example = new LoginLogExample();
        example.createCriteria().andUserIdEqualTo(userId);
        example.setOrderByClause(AdminConstant.CREATE_TIME + AdminConstant.DESC);
        example.setLimitStart(0);
        example.setLimitEnd(1);
        List<LoginLog> returnList = loginLogMapper.selectByExample(example);
        if (CollectionUtils.isNotEmpty(returnList)) {
            loginLog = returnList.get(0);
        }
        return loginLog;
    }

}